package o;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.CancellationSignal;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.appcompat.widget.ActivityChooserView.ActivityChooserViewAdapter;
import androidx.core.content.res.FontResourcesParserCompat.a;
import androidx.core.provider.FontsContractCompat.b;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;

@RestrictTo({d.ॱ})
class gb {
    private ConcurrentHashMap<Long, androidx.core.content.res.FontResourcesParserCompat.e> ˊ = new ConcurrentHashMap();

    interface e<T> {
        boolean ˎ(T t);

        int ॱ(T t);
    }

    gb() {
    }

    private static <T> T ˋ(T[] tArr, int i, e<T> eVar) {
        boolean z;
        int i2 = (i & 1) == 0 ? 400 : 700;
        if ((i & 2) != 0) {
            z = true;
        } else {
            z = false;
        }
        T t = null;
        int i3 = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        int length = tArr.length;
        int i4 = 0;
        while (i4 < length) {
            int i5;
            T t2;
            T t3 = tArr[i4];
            int abs = Math.abs(eVar.ॱ(t3) - i2) * 2;
            if (eVar.ˎ(t3) == z) {
                i5 = 0;
            } else {
                i5 = 1;
            }
            i5 += abs;
            if (t == null || r3 > i5) {
                i3 = i5;
                t2 = t3;
            } else {
                t2 = t;
            }
            i4++;
            t = t2;
        }
        return t;
    }

    private static long ˊ(@Nullable Typeface typeface) {
        if (typeface == null) {
            return 0;
        }
        try {
            Field declaredField = Typeface.class.getDeclaredField("native_instance");
            declaredField.setAccessible(true);
            return ((Number) declaredField.get(typeface)).longValue();
        } catch (Throwable e) {
            Log.e("TypefaceCompatBaseImpl", "Could not retrieve font from family.", e);
            return 0;
        } catch (Throwable e2) {
            Log.e("TypefaceCompatBaseImpl", "Could not retrieve font from family.", e2);
            return 0;
        }
    }

    protected b ˎ(b[] bVarArr, int i) {
        return (b) ˋ(bVarArr, i, new e<b>(this) {
            final /* synthetic */ gb ˋ;

            {
                this.ˋ = r1;
            }

            public int ॱ(b bVar) {
                return bVar.ˏ();
            }

            public boolean ˎ(b bVar) {
                return bVar.ˋ();
            }
        });
    }

    protected Typeface ˊ(Context context, InputStream inputStream) {
        Typeface typeface = null;
        File ˎ = gh.ˎ(context);
        if (ˎ != null) {
            try {
                if (gh.ˎ(ˎ, inputStream)) {
                    typeface = Typeface.createFromFile(ˎ.getPath());
                    ˎ.delete();
                }
            } catch (RuntimeException e) {
            } finally {
                ˎ.delete();
            }
        }
        return typeface;
    }

    @Nullable
    public Typeface ˋ(Context context, @Nullable CancellationSignal cancellationSignal, @NonNull b[] bVarArr, int i) {
        Closeable openInputStream;
        Throwable th;
        Typeface typeface = null;
        if (bVarArr.length >= 1) {
            try {
                openInputStream = context.getContentResolver().openInputStream(ˎ(bVarArr, i).ˊ());
                try {
                    typeface = ˊ(context, (InputStream) openInputStream);
                    gh.ˎ(openInputStream);
                } catch (IOException e) {
                    gh.ˎ(openInputStream);
                    return typeface;
                } catch (Throwable th2) {
                    th = th2;
                    gh.ˎ(openInputStream);
                    throw th;
                }
            } catch (IOException e2) {
                openInputStream = typeface;
                gh.ˎ(openInputStream);
                return typeface;
            } catch (Throwable th3) {
                Throwable th4 = th3;
                openInputStream = typeface;
                th = th4;
                gh.ˎ(openInputStream);
                throw th;
            }
        }
        return typeface;
    }

    private a ˊ(androidx.core.content.res.FontResourcesParserCompat.e eVar, int i) {
        return (a) ˋ(eVar.ˊ(), i, new e<a>(this) {
            final /* synthetic */ gb ˎ;

            {
                this.ˎ = r1;
            }

            public /* synthetic */ int ॱ(Object obj) {
                return ˋ((a) obj);
            }

            public int ˋ(a aVar) {
                return aVar.ॱ();
            }

            public boolean ˎ(a aVar) {
                return aVar.ˏ();
            }
        });
    }

    @Nullable
    public Typeface ॱ(Context context, androidx.core.content.res.FontResourcesParserCompat.e eVar, Resources resources, int i) {
        a ˊ = ˊ(eVar, i);
        if (ˊ == null) {
            return null;
        }
        Typeface ˏ = ft.ˏ(context, resources, ˊ.ʼ(), ˊ.ˊ(), i);
        ˋ(ˏ, eVar);
        return ˏ;
    }

    @Nullable
    public Typeface ॱ(Context context, Resources resources, int i, String str, int i2) {
        Typeface typeface = null;
        File ˎ = gh.ˎ(context);
        if (ˎ != null) {
            try {
                if (gh.ˎ(ˎ, resources, i)) {
                    typeface = Typeface.createFromFile(ˎ.getPath());
                    ˎ.delete();
                }
            } catch (RuntimeException e) {
            } finally {
                ˎ.delete();
            }
        }
        return typeface;
    }

    private void ˋ(Typeface typeface, androidx.core.content.res.FontResourcesParserCompat.e eVar) {
        long ˊ = ˊ(typeface);
        if (ˊ != 0) {
            this.ˊ.put(Long.valueOf(ˊ), eVar);
        }
    }
}
